//
//  PlatformMsgDetailViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/8/5.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "TT_BaseNaviView.h"
#import "CommonMessageModel.h"

#import "PlatformMsgDetailViewController.h"

@interface PlatformMsgDetailViewController ()

@property (nonatomic, strong) TT_BaseNaviView *naviView;

@property (weak, nonatomic) IBOutlet UITextView *contentTV;

@property (nonatomic, strong) CommonMessageModel *model;

@end

@implementation PlatformMsgDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"平台消息详情";
    
    [self initView];
    [self request_sysMsg];
}

- (void)initView {
    
    //屏幕常亮
    [UIApplication sharedApplication].idleTimerDisabled = YES;
    self.navigationController.navigationBarHidden = YES;
    #if RT_INTERACTIVE_PUSH
        self.rt_navigationController.rt_enableInteractivePush = YES;
    #endif
    
    [self.view addSubview:self.naviView];
    [self.naviView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(NavBar_Height + kStatusBarHeight);

    }];
    
}

- (TT_BaseNaviView *)naviView {
    if (!_naviView) {
        _naviView = [[NSBundle mainBundle] loadNibNamed:@"TT_BaseNaviView"owner:nil options:nil].firstObject;
        [_naviView.backBtn setImage:[UIImage imageNamed:@"public_back_black"] forState:UIControlStateNormal];
        _naviView.naviBgImageView.backgroundColor = UIColor.whiteColor;
        [_naviView.backBtn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    }
    return _naviView;
}

- (void)goBack {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)request_sysMsg {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:[TTUserModel sharedObject].userId forKey:@"userId"];
    [dic setValue:self.snId forKey:@"snId"];

    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_SysMessageDetail];
    
    WEAKSELF
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        if ([data isKindOfClass:[NSDictionary class]]) {

            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                NSDictionary *resultDic = [data valueForKey:@"data"];

                weakSelf.model = [CommonMessageModel modelWithDictionary:resultDic];
                
                weakSelf.naviView.titleLab.text = weakSelf.model.noticeTitle;
                weakSelf.contentTV.text = weakSelf.model.noticeContent;
                
                if ([TTUserModel sharedObject].userId.length > 0) {
                    [weakSelf request_readSysMsg];
                }
            }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {

    }];
}

//告诉接口这个消息已读
- (void)request_readSysMsg {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:[TTUserModel sharedObject].userId forKey:@"userId"];
    [dic setValue:self.snId forKey:@"snId"];
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_ReadSysMsg];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        if ([data isKindOfClass:[NSDictionary class]]) {
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                [[TT_MessageManager manager] refreshUnReadCount];
            }
        }
    } FailedBlock:^(NSError * _Nonnull error) {

    }];
}



@end
